import {
  Complex
} from '../api/complex'

class KeyWordModel extends Complex {
  key = 'q'
  maxLength = 10
  getHistory () {
    const words = JSON.parse(localStorage.getItem(this.key))
    if (!words) {
      return []
    }
    return words
  }

  getHot () {
    return this.$axios({
      url: '/book/hot_keyword'
    })
  }

  addToHistory (keyword) {
    let words = this.getHistory()
    const has = words.includes(keyword)
    if (!!keyword && !has) {
      const length = words.length
      if (length >= this.maxLength) {
        words.pop()
      }
      words.unshift(keyword)
      localStorage.setItem(this.key, JSON.stringify(words))
    }
  }
}

export {
  KeyWordModel
}
